#include "tinyxml.h"
#include "stdafx.h"
#include "collada.h"

#ifdef _DEBUG
#define DEBUG_NEW new( _NORMAL_BLOCK, __FILE__, __LINE__ )
#define new DEBUG_NEW
#endif

using namespace Collada;

IMPLEMENT_RTTI( Collada::Data, Collada::Element )
IMPLEMENT_ELEMENT( Collada::Data )

Data::Data( const std::string& id, const std::string& name )
:	Element( id, name )
,	_pScene( NULL )
{
	CHILD_ELEMENT_BEGIN()
		CHILD_ELEMENT( "asset", Collada::Empty )
		CHILD_ELEMENT( "library_geometries", Collada::Library )
		CHILD_ELEMENT( "library_visual_scenes", Collada::Library )
		CHILD_ELEMENT( "scene", Collada::Scene )
		CHILD_ELEMENT( "extra", Collada::Empty )
	CHILD_ELEMENT_END()
}

Data::~Data()
{
	LibraryMgrIter it = _libraryMgr.begin();
	for( ; it != _libraryMgr.end(); ++it )
	{
		LibraryListIter iter = ( it->second ).begin();

		for( ; iter != ( it->second ).end(); ++iter )
		{
			if( *iter )
			{
				delete ( *iter );
			}
		}
	}
	if( _pScene )
	{
		delete _pScene;
		_pScene = NULL;
	}
}

bool Data::AddLibrary( ID libraryId, Element* lib )
{
	if( _libraryMgr.find( libraryId ) == _libraryMgr.end() )
	{
		assert( false && "Wrong Collada format!");
		return false;
	}
	_libraryMgr[ libraryId ].push_back( lib );
	return true;
}

void Data::AllChildElementsLoaded( Element* pParent )
{
}

Geometry* Data::GetGeometry( ID geomId )
{
	ID id = Fundamental::JenkinsPHF( "library_geometries" );
	LibraryListIter it = _libraryMgr[ id ].begin();

	for( ; it != _libraryMgr[ id ].end(); ++it )
	{
		if( ( *it )->GetID() == geomId )
		{
			assert( ( *it )->IsA( Geometry::GetTypeInfo() ) );
			return ( Geometry* )( *it );
		}
	}
	return NULL;
}

VisualScene* Data::GetVisualScene( ID visualSceneId )
{
	ID libraryId = Fundamental::JenkinsPHF( "library_visual_scenes" );
	std::vector< Element* >::iterator it = _libraryMgr[ libraryId ].begin();
	
	for( ; it != _libraryMgr[ libraryId ].end(); ++it )
	{
		if( ( *it )->GetID() == visualSceneId )
		{
			assert( ( *it )->IsA( VisualScene::GetTypeInfo() ) );
			return ( VisualScene* )( *it );
		}
	}
	return NULL;
}

void Data::Initialize( Element* pParent, const void* desc )
{
	TiXmlElement* pChild = ( ( TiXmlElement* )desc )->FirstChildElement(); 
	for( ; pChild; pChild = pChild->NextSiblingElement() )
	{
		const std::string& elementName = pChild->Value();
		if( elementName == "asset" )
		{
		}
		if( elementName == "extra" )
		{
		}
		if( elementName == "scene" )
		{
		}
		else
		{
			ID libraryId = Fundamental::JenkinsPHF( elementName );
			_libraryMgr[ libraryId ];
		}
	}
}

bool Data::IsValid( const void* desc )
{
	if( desc == NULL )
	{
		return false;
	}

	TiXmlElement* pElem = ( TiXmlElement* )desc;
	if( std::string( pElem->Value() ) != "COLLADA" )
	{
		return false;
	}

	if( ( pElem = pElem->FirstChildElement() ) == NULL )
	{
		return false;
	}

	if( std::string( pElem->Value() ) != "asset" )
	{
		return false;
	}

	return true;
}